var showchaptertype = "";
var showmorechapter = false;
var firstload = true;
$(function () {
    $("#checkAdult").click(function () {
        $.cookie("isAdult", 1, { path: "/", expires: 1 });
        window.location.reload();
    });

    var detailSwiper = new Swiper('.sub-bar1 .slide .swiper-container', {});
    var detailSwiper2 = new Swiper('.sub-bar2 .slide .swiper-container', {});
    $('.sub-bar1 .slide .swiper-container').parent().find('.left-arrow').on('click', function (e) {
        e.preventDefault();
        detailSwiper.swipePrev();
        firstload = false;
        var mid = $('.sub-bar1 .swiper-slide-active .subtitle').attr('mid');
        var favBtn = $(this).parents('.slide').next('a')
        if (("#btncollection").length > 0) {
            $.ajax({
                url: 'userdata.ashx?d=' + new Date().getTime(),
                dataType: 'json',
                data: { tp: 7, mid: mid },
                type: 'POST',
                success: function (data) {
                    if (data && data.msg == "1") {
                        favBtn.addClass("active");
                        favBtn.html('<i class="icon icon-fav2-full"></i>已收藏');
                    }
                    else {
                        favBtn.removeClass("active");
                        favBtn.html('<i class="icon icon-fav2"></i>收藏');
                    }
                }
            });
        }
    });
    $('.sub-bar1 .slide .swiper-container').parent().find('.right-arrow').on('click', function (e) {
        e.preventDefault();
        detailSwiper.swipeNext();
        firstload = false;
        var mid = $('.sub-bar1 .swiper-slide-active .subtitle').attr('mid');
        var favBtn = $(this).parents('.slide').next('a')
        if (("#btncollection").length > 0) {
            $.ajax({
                url: 'userdata.ashx?d=' + new Date().getTime(),
                dataType: 'json',
                data: { tp: 7, mid: mid },
                type: 'POST',
                success: function (data) {
                    if (data && data.msg == "1") {
                        favBtn.addClass("active");
                        favBtn.html('<i class="icon icon-fav2-full"></i>已收藏');
                    }
                    else {
                        favBtn.removeClass("active");
                        favBtn.html('<i class="icon icon-fav2"></i>收藏');
                    }
                }
            });
        }
    });
    $('.sub-bar2 .slide .swiper-container').parent().find('.left-arrow').on('click', function (e) {
        e.preventDefault();
        detailSwiper2.swipePrev();
        firstload = false;
        var mid = $('.sub-bar2 .swiper-slide-active .subtitle').attr('mid');
        var favBtn = $(this).parents('.slide').next('a')
        if (("#btncollection2").length > 0) {
            $.ajax({
                url: 'userdata.ashx?d=' + new Date().getTime(),
                dataType: 'json',
                data: { tp: 7, mid: mid },
                type: 'POST',
                success: function (data) {
                    if (data && data.msg == "1") {
                        favBtn.addClass("active");
                        favBtn.html('<i class="icon icon-fav2-full"></i>已收藏');
                    }
                    else {
                        favBtn.removeClass("active");
                        favBtn.html('<i class="icon icon-fav2"></i>收藏');
                    }
                }
            });
        }
    });
    $('.sub-bar2 .slide .swiper-container').parent().find('.right-arrow').on('click', function (e) {
        e.preventDefault();
        detailSwiper2.swipeNext();
        firstload = false;
        var mid = $('.sub-bar2 .swiper-slide-active .subtitle').attr('mid');
        var favBtn = $(this).parents('.slide').next('a')
        if (("#btncollection2").length > 0) {
            $.ajax({
                url: 'userdata.ashx?d=' + new Date().getTime(),
                dataType: 'json',
                data: { tp: 7, mid: mid },
                type: 'POST',
                success: function (data) {
                    if (data && data.msg == "1") {
                        favBtn.addClass("active");
                        favBtn.html('<i class="icon icon-fav2-full"></i>已收藏');
                    }
                    else {
                        favBtn.removeClass("active");
                        favBtn.html('<i class="icon icon-fav2"></i>收藏');
                    }
                }
            });
        }
    });
    $(".fold_open").click(
        function () {
            $(this).text("[+展开]");
            $(this).next().show();
            $(this).hide();
            $(".banner_detail_form .info .bottom").css({ "position": "relative", "bottom": "0px", "margin-bottom": "25px" });
        }
    );
    $(".fold_close").click(
        function () {
            $(this).text("[-折叠]");
            $(this).parent().hide();
            $(this).parent().prev().show();
            $(".banner_detail_form .info .bottom").css({ "position": "absolute", "bottom": "30px", "margin-bottom": "0px" });
        }
    );
    shareFormHide();
    if ($("#btncollection").length > 0) {
        var mid = $('.sub-bar1 .swiper-slide .subtitle').eq(1).attr('mid');
        $.ajax({
            url: 'userdata.ashx?d=' + new Date().getTime(),
            dataType: 'json',
            data: { tp: 7, mid: mid },
            type: 'POST',
            success: function (data) {
                if (data && data.msg == "1") {
                    $("#btncollection").addClass("active");
                    $("#btncollection").html('<i class="icon icon-fav2-full"></i>已收藏');
                }
            }
        });
    }
    $("#btncollection").click(function () {
        var mid = $('.sub-bar1 .swiper-slide-active .subtitle').attr('mid');
        if (firstload) {
            mid = $('.sub-bar1 .swiper-slide .subtitle').eq(1).attr('mid');
        }
        var uid = DM5_USERID;
        if (uid == 0) {
            showlogin();
        }
        else {
            var t = $(this);
            var cancel = 0;
            if (t.length > 0) {
                if (t.hasClass("active")) {
                    cancel = 1;
                }
            }
            $.ajax({
                url: 'bookmarker.ashx?d=' + new Date().getTime(),
                dataType: 'json',
                data: { cid: 0, mid: mid, page: 0, uid: uid, language: 1, cancel: cancel },
                type: 'POST',
                success: function (msg) {
                    if (msg.Value == "1") {
                        if (t.length > 0) {
                            if (t.hasClass("active")) {
                                t.removeClass("active");
                                t.html('<i class="icon icon-fav2"></i>收藏')
                            } else {
                                t.addClass("active");
                                t.html('<i class="icon icon-fav2-full"></i>已收藏')
                            }
                        }
                        if (cancel == 1) {
                            ShowDialog("取消收藏成功");
                        }
                        else {
                            ShowDialog("收藏成功");
                        }
                    }
                    else if (msg.Value == "2") {
                        ShowDialog("操作失败！已收藏");
                    }
                    else {
                        if (cancel == 1) {
                            ShowDialog("取消收藏失败");
                        }
                        else {
                            ShowDialog("收藏失败");
                        }
                    }
                }
            });
        }
    });

    if ($("#btncollection2").length > 0) {
        var mid = $('.sub-bar2 .swiper-slide .subtitle').eq(1).attr('mid');
        $.ajax({
            url: 'userdata.ashx?d=' + new Date().getTime(),
            dataType: 'json',
            data: { tp: 7, mid: mid },
            type: 'POST',
            success: function (data) {
                if (data && data.msg == "1") {
                    $("#btncollection2").addClass("active");
                    $("#btncollection2").html('<i class="icon icon-fav2-full"></i>已收藏');
                }
            }
        });
    }
    $("#btncollection2").click(function () {
        var mid = $('.sub-bar2 .swiper-slide-active .subtitle').attr('mid');
        if (firstload) {
            mid = $('.sub-bar2 .swiper-slide .subtitle').eq(1).attr('mid');
        }
        var uid = DM5_USERID;
        if (uid == 0) {
            showlogin();
        }
        else {
            var t = $(this);
            var cancel = 0;
            if (t.length > 0) {
                if (t.hasClass("active")) {
                    cancel = 1;
                }
            }
            $.ajax({
                url: 'bookmarker.ashx?d=' + new Date().getTime(),
                dataType: 'json',
                data: { cid: 0, mid: mid, page: 0, uid: uid, language: 1, cancel: cancel },
                type: 'POST',
                success: function (msg) {
                    if (msg.Value == "1") {
                        if (t.length > 0) {
                            if (t.hasClass("active")) {
                                t.removeClass("active");
                                t.html('<i class="icon icon-fav2"></i>收藏')
                            } else {
                                t.addClass("active");
                                t.html('<i class="icon icon-fav2-full"></i>已收藏')
                            }
                        }
                        if (cancel == 1) {
                            ShowDialog("取消收藏成功");
                        }
                        else {
                            ShowDialog("收藏成功");
                        }
                    }
                    else if (msg.Value == "2") {
                        ShowDialog("操作失败！已收藏");
                    }
                    else {
                        if (cancel == 1) {
                            ShowDialog("取消收藏失败");
                        }
                        else {
                            ShowDialog("收藏失败");
                        }
                    }
                }
            });
        }
    });

    $(".detail-list-select").each(function () {
        if (!$(this).is(":hidden")) {
            var chapteritem = $(this).find(".chapteritem");
            if (chapteritem.length > 0) {
                if (chapteritem.is(":hidden")) {
                    $(".detail-more").show();
                }
                else {
                    $(".detail-more").hide();
                }
            }
            else {
                $(".detail-more").hide();
            }
            return;
        }
    });
});

function sortBtnClick(item) {
    $(item).toggleClass("sortBak");
    if (DM5_COMIC_SORT == 1) {
        DM5_COMIC_SORT = 2;
        $(item).addClass('inverted').text('倒序');
        ShowDialog("新章节在前");
    }
    else {
        DM5_COMIC_SORT = 1;
        $(item).removeClass('inverted').text('正序');
        ShowDialog("新章节在后");
    }
    //$("#chapterlistload").load("加载中...");
    //$('.detail-list-title a').eq(0).click();
    $("#chapterlistload").load("/template-" + DM5_COMIC_MID + "-s" + DM5_COMIC_SORT + "/");
}

String.prototype.startWith = function (str) {
    var reg = new RegExp("^" + str);
    return reg.test(this);
}


function titleSelect(item, className, id) {
    if ($('.' + className).length > 1) {
        $(item).parent().find('a').removeClass('active');
        $(item).addClass('active');
    }
    $('.' + className).hide();
    $('#' + id).show();
    showchaptertype = id;
    var chapteritem = $('#' + id).find(".chapteritem");
    if (chapteritem.length > 0) {
        if (chapteritem.is(":hidden")) {
            $(".detail-more").show();
        }
        else {
            $(".detail-more").hide();
        }
    }
    else {
        $(".detail-more").hide();
    }
}
function getTextCount(item) {
    $(item).parent().find('.red').text(180 - $(item).val().length);
}
function ShowDialog(title) {
    $("#alertTop_1").text(title);
    $(".alertTop_1").show();
    setTimeout(function () {
        alertTopHide_1();
    }, 1000);
}
function alertTopHide_1() {
    $(".alertTop_1").hide();
}

function shareFormHide() {
    $("body").click(function (event) {
        if (event.target.id != "shareForm" && event.target.id != "share") {
            $("#shareForm").hide();
        }
    });
}

//章节加载更多事件
function charpterMore(item) {
    $(".chapteritem").show();
    $(item).remove();
    showmorechapter = true;
}